package com.team2.cyber_eye;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import com.team2.cyber_eye.ActivityHelpers.AdapterProduct;
import com.team2.cyber_eye.ActivityHelpers.AdapterStore;
import com.team2.cyber_eye.ActivityHelpers.ProductSortComparator;
import com.team2.cyber_eye.ActivityHelpers.Util;
import com.team2.shared.Product;
import com.team2.shared.Store;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author mwho
 *
 */
public class OnlineProductList extends Fragment {
	
	ListView list;
	AdapterProduct adapter;
	ArrayList<Product> products;
	/* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (container == null) {
            return null;
        }
		
		View view = (LinearLayout)inflater.inflate(R.layout.activity_online_product_list, container, false);
		
		Bundle args = this.getArguments();
		
		if(args != null){
			products = new ArrayList<Product>();
			try
			{
				products = (ArrayList<Product>)(Object)args.getParcelableArrayList("products");
			}
			catch(Exception ex) {
				
			}
			
			list=(ListView)view.findViewById(R.id.productOnlineList);
		    adapter=new AdapterProduct(getActivity(), products);
		    
		    
		    list.setAdapter(adapter);
		    list.setOnItemClickListener(new OnItemClickListener() { 
		    	@Override public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
		    		ItemClick(position); 
		    		} 
		    	});
		}
		
		Spinner spinnerCategory = (Spinner) view.findViewById(R.id.categorySpinner);
		ArrayAdapter<CharSequence> adapterCategory = ArrayAdapter.createFromResource(getActivity(), R.array.sorting_array, android.R.layout.simple_spinner_item);
		adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerCategory.setAdapter(adapterCategory);
		Util.SetSpinnerTo(spinnerCategory, Options.GetProductSort());
		
		spinnerCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int pos,
					long id) {
			
				CharSequence text = parent.getItemAtPosition(pos).toString();
				/*int duration = Toast.LENGTH_SHORT;

				Toast toast = Toast.makeText(getActivity(), text, duration);
				toast.show();*/
				
				ProductSortComparator obj = new ProductSortComparator();
				
				obj.Sort = text.toString();
				
				if (products != null)
					Collections.sort(products, obj);
								
				adapter=new AdapterProduct(getActivity(), products);
			    
			    list.setAdapter(adapter);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub				
			}

		});
		return view;
	}
	
	protected void ItemClick(int position)
	{
		try
		{
			Product p = (Product)adapter.getItem(position);
			Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(p.getLink()));
			startActivity(browserIntent);
			
		}
		catch (Exception ex)
		{
			Util.DisplayError(getActivity(), "Error loading product info", ex);
		}
	}
}

